package com.ruoyi.weiriya.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.WVehicle;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.weiriya.domain.WArea;
import com.ruoyi.weiriya.domain.WNonVipSetting;
import com.ruoyi.weiriya.domain.WVipSetting;
import com.ruoyi.weiriya.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.weiriya.mapper.WVehicleNonChargeTypeMapper;
import com.ruoyi.weiriya.domain.WVehicleNonChargeType;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 车辆非充电类型Service业务层处理
 * 
 * @author lynn
 * @date 2023-08-01
 */
@Service
public class WVehicleNonChargeTypeServiceImpl extends ServiceImpl<WVehicleNonChargeTypeMapper, WVehicleNonChargeType> implements IWVehicleNonChargeTypeService
{

    @Autowired
    private IWVipSettingService vipSettingService;

    @Autowired
    private IWAreaService areaService;

    @Autowired
    private IWVehicleService vehicleService;

    @Override
    public boolean removeTypeByIds(List<String> ids) {

        List<WVehicle> list = vehicleService.list(new LambdaQueryWrapper<WVehicle>()
                .in(WVehicle::getVehicleNonChargeTypeId, ids));
        if(list.size() > 0){
            throw new ServiceException("类型已被使用，禁止删除!");
        }
        return this.removeByIds(ids);
    }

    @Override
    public boolean saveType(WVehicleNonChargeType wVehicleNonChargeType) {
        List<WVehicleNonChargeType> list = this.list(wVehicleNonChargeType.queryWrapper());
        if(list.size() > 0){
            throw new ServiceException("类型已存在！");
        }

        this.save(wVehicleNonChargeType);

        // 为点位添加类型
        // 获取所有点位
        List<String> areaIds = areaService.list().stream().map(WArea::getId).collect(Collectors.toList());
        ArrayList<WVipSetting> settingList = new ArrayList<>();
        for (String areaId : areaIds) {
            WVipSetting wVipSetting = new WVipSetting();
            wVipSetting.setAreaId(areaId);
            wVipSetting.setSettingType(2);
            wVipSetting.setVehicleType(wVehicleNonChargeType.getId());
            wVipSetting.setMonthAmount(0D);
            wVipSetting.setQuarterAmount(0D);
            wVipSetting.setYearAmount(0D);
            settingList.add(wVipSetting);
        }
        vipSettingService.saveBatch(settingList);
        return true;
    }
}
